#include <stdio.h>
#include <stdlib.h>
#include "./PriorityQueue.h"

void checkEmptyPointer(p){
	if(!p){
		printf("Error: Bad Memory Allocation!\n");
		exit(-1);
	}
}
/**
 * numberOfPriorities: number of queues the PQ consist
 * return: A pointer to a Queue array, or -1 for error.
 */
Queue * createPriorityQueues(int numberOfPriorities){
	queueSize allocateSize = sizeof(Queue) * numberOfPriorities;
	Queue * p = (Queue *)malloc(sizeof(Queue) * numberOfPriorities);
	checkEmptyPointer(p);
	memset(p, 0, allocateSize);
	return p;
}

/**
 * Put the item at the head of PQ[prio].pItems.
 * If usedSize is equal to capacity, then allocate double size of memory.
 */
int putHeadOfQueue(Queue * pQ, itemType item, priority prio){
	checkEmptyPointer(pQ);
	struct Queue queue = pQ[prio];
	/*
	 * If the space has been used up, allocate twice size of that.
	 */
	if(queue.capacity == queue.usedSize){
		if(queue.capacity == 0) {/* first allcation: 8 */
			queue.pItems = (itemType *)malloc(sizeof(itemType) * 8);
			checkEmptyPointer(queue.pItems);
			queue.capacity = 8;
			queue.readIndex = queue.usedSize = \
			queue.hightestAvailable = queue.lowestAvailable = 0;
		}
		else{
			queue.pItems = realloc(void *ptr, queue.capacity * sizeof(Queue) * 2);
			checkEmptyPointer(queue.pItems);
			queue.capacity *= 2;
		}
	}
	
	/*
	 * Adjustion the readIndex & the others
	 */
}

/**
 * delete a PQ and free the memory.
 */
void * deleteQueue(Queue * pQ, priority priorityQueueSize){
	int prio = 0;
	for(prio = 0; prio < priorityQueueSize; prio++){
		free(pQ[prio].pItems);
		pQ[prio].pItems = 0;
	}
	free(pQ);
}